package com.yand.frame.controller.system.role;


import com.github.pagehelper.PageInfo;
import com.yand.frame.common.Result;
import com.yand.frame.entity.ActivityMenu;
import com.yand.frame.entity.ActivityRole;
import com.yand.frame.service.LoginService;
import com.yand.frame.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {


    @Autowired
    private RoleService roleService;



    /**
     * 获取角色列表分页展示
     * @param model
     * @return
     */
    @RequestMapping("/roleList")
    public String toPage(Model model, ActivityRole role){

        PageInfo pageInfo = roleService.queryAllRole(role);

        model.addAttribute("pageInfo",pageInfo);

        return "role/role-list";
    }


    /**
     * 跳转角色的修改的页面
     * @param roleId 角色id
     * @param model
     * @return
     */
    @GetMapping("/update/{roleId}")
    public String toEditPage(@PathVariable(value = "roleId") String roleId, Model model){

        ActivityRole role = roleService.getRole(Integer.parseInt(roleId));

        model.addAttribute("role", role);
        return "role/role-edit";
    }

    @PostMapping("/update")
    @ResponseBody
    public Result udpateRole(ActivityRole role){
        roleService.updateRole(role);
        return new Result(0);
    }



    /**
     * 添加角色的方法
     * @param role
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result addRole(ActivityRole role){
        //声明一个返回对象
        Result result = new Result();

        roleService.addRole(role);

        return new Result(0);
    }


    /**
     * 添加角色的方法
     * @param role
     * @return
     */
    @PostMapping("/delete/{roleId}")
    @ResponseBody
    public Result delRole(@PathVariable(value = "roleId") String roleId){
        //声明一个返回对象
        Result result = new Result();

        roleService.delRole(Integer.parseInt(roleId));

        return new Result(0);
    }




    /**
     * 跳转角色的增加的页面
     *
     * @return 角色添加页面
     */
    @GetMapping("/add")
    public String toAddPage(){

        //跳转角色添加页面
        return "role/role-add";
    }


    /**
     * 根据当前角色名称获取角色所绑定的菜单
     * @param roleId
     * @return
     */
    @RequestMapping("/getRoleMenu/{roleId}")
    @ResponseBody
    public List<ActivityMenu> getMenuByRoleId(@PathVariable(value = "roleId") String roleId){

        List<ActivityMenu> menuList = roleService.getMenuByRoleId(Integer.parseInt(roleId));

        return menuList;
    }





}
